package com.songxh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songxh.common.CustomException;
import com.songxh.mapper.CategoryMapper;
import com.songxh.pojo.Category;
import com.songxh.pojo.Dish;
import com.songxh.pojo.Setmeal;
import com.songxh.service.CategoryService;
import com.songxh.service.DishService;
import com.songxh.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author songxh
 * @create 2022-04-24-18:19
 */
@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id来删除套餐或者菜品，但是需要判断该菜品是否有关联
     * @param id
     */
    @Override
    public void remove(long id) {
        QueryWrapper<Dish> queryWrapper=new QueryWrapper<>();
        //查询当前分类是否关联了菜品，如果是，则抛出一个业务异常
        queryWrapper.eq("category_id",id);
        int dish = dishService.count(queryWrapper);
        if (dish>0){
            //抛出一个业务异常
            throw new CustomException("当前分类关联了菜品，不能删除");
        }

        //查询当前分类是否关联了套餐，如果是，抛异常
        QueryWrapper<Setmeal> setmealQueryWrapper=new QueryWrapper<>();
        setmealQueryWrapper.eq("category_id",id);
        int setmeal = setmealService.count(setmealQueryWrapper);
        if (setmeal>0){
            //抛出一个业务异常
            throw new CustomException("当前分类关联了套餐，不能删除");
        }


        //正常删除
        super.removeById(id);

    }
}
